import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**军团属性枚举*/
export const enum E_LegionSnapshot {
	/** 军团ID*/
	LEGION_ID = 0,
	/** 军团名*/
	LEGION_NAME = 1,
	/** 军团等级*/
	LEGION_LEVEL = 2,
	/** 军团战力*/
	LEGION_COMBAT_POWER = 3,
	/** 总成员数*/
	TOTAL_MEMBER = 4,
	/** 军团旗帜*/
	LEGION_FLAG = 5,
	/**枚举数量*/
	EnumCount = 6
}


/**军团*/
export class LegionSnapshot {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_LEGION_SNAPSHOT_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_LEGION_SNAPSHOT_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: LegionSnapshot[] = [];

	/**创建入口*/
	public static create(json?: string): LegionSnapshot {
		const c = this.POOL.pop() || new LegionSnapshot();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/** 军团ID (int32)*/
	protected _legionId: number;
	/** 军团名*/
	protected _legionName: string;
	/** 军团等级 (long)*/
	protected _legionLevel: number;
	/** 军团战力 (uint32)*/
	protected _legionCombatPower: number;
	/** 总成员数 (uint32)*/
	protected _totalMember: number;
	/** 军团旗帜 (uint32)*/
	protected _legionFlag: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (LegionSnapshot.POOL.indexOf(this) === -1) {
			LegionSnapshot.POOL.push(this);
		}
	}

	/** 军团ID*/
	public get legionId() {
		return this._legionId;
	}


	/** 军团ID*/
	public set legionId(v: number) {
		this._legionId = v;
	}


	/** 军团名*/
	public get legionName() {
		return this._legionName;
	}


	/** 军团名*/
	public set legionName(v: string) {
		this._legionName = v;
	}


	/** 军团等级*/
	public get legionLevel() {
		return this._legionLevel;
	}


	/** 军团等级*/
	public set legionLevel(v: number) {
		this._legionLevel = v;
	}


	/** 军团战力*/
	public get legionCombatPower() {
		return this._legionCombatPower;
	}


	/** 军团战力*/
	public set legionCombatPower(v: number) {
		this._legionCombatPower = v;
	}


	/** 总成员数*/
	public get totalMember() {
		return this._totalMember;
	}


	/** 总成员数*/
	public set totalMember(v: number) {
		this._totalMember = v;
	}


	/** 军团旗帜*/
	public get legionFlag() {
		return this._legionFlag;
	}


	/** 军团旗帜*/
	public set legionFlag(v: number) {
		this._legionFlag = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_LegionSnapshot): any {
		switch (k) {
			case E_LegionSnapshot.LEGION_ID: return this._legionId;
			case E_LegionSnapshot.LEGION_NAME: return this._legionName;
			case E_LegionSnapshot.LEGION_LEVEL: return this._legionLevel;
			case E_LegionSnapshot.LEGION_COMBAT_POWER: return this._legionCombatPower;
			case E_LegionSnapshot.TOTAL_MEMBER: return this._totalMember;
			case E_LegionSnapshot.LEGION_FLAG: return this._legionFlag;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_LegionSnapshot, v: any): void {
		switch (k) {
			case E_LegionSnapshot.LEGION_ID: this._legionId = v; break;
			case E_LegionSnapshot.LEGION_NAME: this._legionName = v; break;
			case E_LegionSnapshot.LEGION_LEVEL: this._legionLevel = v; break;
			case E_LegionSnapshot.LEGION_COMBAT_POWER: this._legionCombatPower = v; break;
			case E_LegionSnapshot.TOTAL_MEMBER: this._totalMember = v; break;
			case E_LegionSnapshot.LEGION_FLAG: this._legionFlag = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		// 军团ID
		this._legionId = 0;
		// 军团名
		this._legionName = '';
		// 军团等级
		this._legionLevel = 0;
		// 军团战力
		this._legionCombatPower = 0;
		// 总成员数
		this._totalMember = 0;
		// 军团旗帜
		this._legionFlag = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._legionId) {
			json.legionId = this._legionId;
		}
		if (this._legionName) {
			json.legionName = this._legionName;
		}
		if (this._legionLevel) {
			json.legionLevel = this._legionLevel;
		}
		if (this._legionCombatPower) {
			json.legionCombatPower = this._legionCombatPower;
		}
		if (this._totalMember) {
			json.totalMember = this._totalMember;
		}
		if (this._legionFlag) {
			json.legionFlag = this._legionFlag;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.legionId) {
			this._legionId = json.legionId;
		}
		if (json.legionName) {
			this._legionName = json.legionName;
		}
		if (json.legionLevel) {
			this._legionLevel = json.legionLevel;
		}
		if (json.legionCombatPower) {
			this._legionCombatPower = json.legionCombatPower;
		}
		if (json.totalMember) {
			this._totalMember = json.totalMember;
		}
		if (json.legionFlag) {
			this._legionFlag = json.legionFlag;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeInt32(this._legionId);
		byte.writeString(this._legionName);
		byte.writeLongNumber(this._legionLevel);
		byte.writeUint32(this._legionCombatPower);
		byte.writeUint32(this._totalMember);
		byte.writeUint32(this._legionFlag);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._legionId = byte.readInt32();
		this._legionName = byte.readString();
		this._legionLevel = byte.readLongNumber();
		this._legionCombatPower = byte.readUint32();
		this._totalMember = byte.readUint32();
		this._legionFlag = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): LegionSnapshot {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = LegionSnapshot.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = LegionSnapshot.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			// 军团ID
			if (idx === E_LegionSnapshot.LEGION_ID) {
				const oldv = this._legionId;
				this._legionId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionId, oldv);
				i += 2;
				continue;
			}
			// 军团名
			if (idx === E_LegionSnapshot.LEGION_NAME) {
				const oldv = this._legionName;
				this._legionName = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionName, oldv);
				i += 2;
				continue;
			}
			// 军团等级
			if (idx === E_LegionSnapshot.LEGION_LEVEL) {
				const oldv = this._legionLevel;
				this._legionLevel = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionLevel, oldv);
				i += 2;
				continue;
			}
			// 军团战力
			if (idx === E_LegionSnapshot.LEGION_COMBAT_POWER) {
				const oldv = this._legionCombatPower;
				this._legionCombatPower = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionCombatPower, oldv);
				i += 2;
				continue;
			}
			// 总成员数
			if (idx === E_LegionSnapshot.TOTAL_MEMBER) {
				const oldv = this._totalMember;
				this._totalMember = us[i + 1];
				IAsync.dispatch(eventID, idx, this._totalMember, oldv);
				i += 2;
				continue;
			}
			// 军团旗帜
			if (idx === E_LegionSnapshot.LEGION_FLAG) {
				const oldv = this._legionFlag;
				this._legionFlag = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionFlag, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in LegionSnapshot with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: LegionSnapshot): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._legionId !== other._legionId) {
			updates.push(E_LegionSnapshot.LEGION_ID, this._legionId);
		}
		if (this._legionName !== other._legionName) {
			updates.push(E_LegionSnapshot.LEGION_NAME, this._legionName);
		}
		if (this._legionLevel !== other._legionLevel) {
			updates.push(E_LegionSnapshot.LEGION_LEVEL, this._legionLevel);
		}
		if (this._legionCombatPower !== other._legionCombatPower) {
			updates.push(E_LegionSnapshot.LEGION_COMBAT_POWER, this._legionCombatPower);
		}
		if (this._totalMember !== other._totalMember) {
			updates.push(E_LegionSnapshot.TOTAL_MEMBER, this._totalMember);
		}
		if (this._legionFlag !== other._legionFlag) {
			updates.push(E_LegionSnapshot.LEGION_FLAG, this._legionFlag);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(LegionSnapshot.TOTAL_EVENT_ID, this.getV(0));
	}

}
